package com.tanglinkai.mall.controller.admin;

import com.tanglinkai.mall.controller.baseControler.ABaseController;
import com.tanglinkai.mall.contstants.Constants;
import com.tanglinkai.mall.entity.po.Category;
import com.tanglinkai.mall.service.CategoryService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/admin/category")
public class CategoryController extends ABaseController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加分类页面
     *
     * @return
     */
    @GetMapping("/add")
    public ModelAndView addPage() {
        return getSuccessResponseVO("category_add");
    }

    /**
     * 处理添加分类请求
     *
     * @param category
     * @param session
     * @return
     */
    @PostMapping("/add")
    public ModelAndView add(Category category, HttpSession session) {
        try {
            if (session.getAttribute(Constants.MANAGER_KEY) == null) {
                return getSuccessResponseVO("redirect:/admin/login");
            }

            categoryService.addCategory(category);
            return getSuccessResponseVO("redirect:/admin/category/list");

        } catch (IllegalArgumentException e) {
            ModelAndView mav = getSuccessResponseVO("category_add");
            mav.addObject("error", e.getMessage());
            return mav;
        }
    }

    /**
     * 分类列表页面
     *
     * @param session
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(HttpSession session) {
        if (session.getAttribute(Constants.MANAGER_KEY) == null) {
            return getSuccessResponseVO("redirect:/admin/login");
        }

        List<Category> categories = categoryService.getAllCategories();
        ModelAndView mav = getSuccessResponseVO("category_list");
        mav.addObject("categories", categories);
        return mav;
    }

    /**
     * 编辑分类页面
     *
     * @param id
     * @param session
     * @return
     */
    @GetMapping("/edit")
    public ModelAndView editPage(@RequestParam Integer id, HttpSession session) {
        if (session.getAttribute(Constants.MANAGER_KEY) == null) {
            return getSuccessResponseVO("redirect:/admin/login");
        }
        Category category = categoryService.getCategoryById(id);
        ModelAndView mav = getSuccessResponseVO("category_edit");
        mav.addObject("category", category);
        return mav;
    }

    /**
     * 处理编辑分类请求
     *
     * @param category
     * @param session
     * @return
     */
    @PostMapping("/edit")
    public ModelAndView edit(Category category, HttpSession session) {
        try {
            if (session.getAttribute(Constants.MANAGER_KEY) == null) {
                return getSuccessResponseVO("redirect:/admin/login");
            }
            categoryService.updateCategory(category);
            return getSuccessResponseVO("redirect:/admin/category/list");
        } catch (IllegalArgumentException e) {
            ModelAndView mav = getSuccessResponseVO("category_edit");
            mav.addObject("error", e.getMessage());
            mav.addObject("category", category);
            return mav;
        }
    }

    /**
     * 删除分类
     *
     * @param id
     * @param session
     * @return
     */
    @GetMapping("/delete")
    public ModelAndView delete(@RequestParam Integer id, HttpSession session) {
        if (session.getAttribute(Constants.MANAGER_KEY) == null) {
            return getSuccessResponseVO("redirect:/admin/login");
        }
        categoryService.deleteCategory(id);
        return getSuccessResponseVO("redirect:/admin/category/list");
    }
}
